import React, { useEffect } from 'react';
import { Button, Checkbox, Form, Input } from 'antd';
import { getTian,getBian } from "../apis/apis";
function Articleadd({isShow,setData,comment,handleCancel}) {
    const [form] = Form.useForm();
    useEffect(()=>{
        if(isShow){
            form.setFieldsValue(comment)

        }else{
            form.resetFields()

        }
    },[isShow])

    const onFinish=async (values) => {
  console.log('Success:', values);
  values.article.tagList=[values.article.tagList]
  values.article.categoryList=[values.article.categoryList]
  if(isShow){
    const res=await getBian(values.article,comment.id)
    console.log('编辑成功');
    setData()
    handleCancel()
    
  }else{
    values.article.authorId=47529555
    const res=await getTian(values.article)
    console.log(res);
    
    console.log('新增成功');
    setData()
    handleCancel()
  }
};

const onFinishFailed= (errorInfo) => {
  console.log('Failed:', errorInfo);
};
    
    return (
        <div>
            <Form
            form={form}
    name="basic"
    labelCol={{ span: 8 }}
    wrapperCol={{ span: 16 }}
    style={{ maxWidth: 600 }}
    initialValues={{ remember: true }}
    onFinish={onFinish}
    onFinishFailed={onFinishFailed}
    autoComplete="off"
  >
    <Form.Item
      label="标题"
      name={["article","title"]}
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input />
    </Form.Item>
    <Form.Item
      label="内容也有"
      name={["article","content"]}
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input />
    </Form.Item>
    <Form.Item
      label="还有分类"
      name={["article","tagList"]}
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input />
    </Form.Item>
    <Form.Item
      label="标签也干了"
      name={["article","categoryList"]}
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input />
    </Form.Item>

    
    <Form.Item label={null}>
      <Button type="primary" htmlType="submit">
        {isShow?'保存编辑':'保存新增'}
      </Button>
    </Form.Item>
  </Form>
        </div>
    );
}

export default Articleadd;